<template>
  <div style="margin-top: -20px">
    <div style="display: flex;margin-bottom: 15px;align-items: center">
      <div>
        <el-button
          type="primary"
          size="mini"
          plain
          @click="fanhui()"
        >
          返回
        </el-button>
      </div>
      <h1 style="margin-left: 15px">更新资产套件工具包</h1>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['picprocess:models:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >追加</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="listDataList" v-loading="loading" :data="listDataList" @selection-change="handleSelectionChange">
      <el-table-column label="型号名称" align="left" prop="model_name" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['picprocess:models:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改vadmin/assets/对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="220px">
        <el-form-item label="资产型号" prop="category">
          <el-select v-model="form.id" placeholder="请选择资产型号" filterable clearable size="small">
            <el-option
              v-for="dict in modelsList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="eol">
          <div style="width: 180px">
            <el-input v-model="form.quantity" placeholder="请输入型号数量" />
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAssets, deleteAssets, getAssets } from "@/api/vadmin/assets/kits";
import { listDepreciations } from "@/api/vadmin/basic/depreciations";
import { listManufacturers } from "@/api/vadmin/basic/manufacturers";
import { listCategories } from "@/api/vadmin/basic/categories";
import { listModels } from "@/api/vadmin/basic/models";

export default {
  name: "Models",
  components: {},
  props: {
    topId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      title1: "",
      open1: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // vadmin/assets/表格数据
      modelsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        kit: null,
        tag: "model"
      },
      // 表单参数
      form: {
        kit: null,
        tag: "model",
        quantity: 2,
        id: null
      },
      listDataList: [],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "型号名称不能为空", trigger: "blur" }
        ],
        model_number: [
          { required: true, message: "型号编码不能为空", trigger: "blur" }
        ],
        // deprecated_mac_address: [
        //   { required: true, message: "MAC地址是否弃用不能为空", trigger: "blur" }
        // ],
        requestable: [
          { required: true, message: "是否可申请不能为空", trigger: "blur" }
        ],
        categoriesList: [],
        depreciationsList: [],
        manufacturersList: []
      }
    };
  },
  mounted() {
    this.form.kit = this.topId;
    this.queryParams.kit = this.topId;
    this.getModelsList();
    this.getList();
  },
  methods: {
    fanhui() {
      this.$emit("fanhui");
    },
    getModelsList() {
      this.loading = true;
      listModels().then(response => {
        this.modelsList = response.data.results;
        this.loading = false;
      });
    },
    getCategoriesList() {
      this.loading = true;
      listCategories().then(response => {
        this.categoriesList = response.data.results;
        this.loading = false;
      });
    },
    getDepreciationsList() {
      this.loading = true;
      listDepreciations().then(response => {
        this.depreciationsList = response.data.results;
        this.loading = false;
      });
    },
    getMaufacturersList() {
      this.loading = true;
      listManufacturers().then(response => {
        this.manufacturersList = response.data.results;
        this.loading = false;
      });
    },
    /** 查询vadmin/assets/列表 */
    getList() {
      this.loading = true;
      getAssets(this.queryParams).then(response => {
        this.listDataList = response.data;
        this.total = response.data.count ? response.data.count : this.listDataList.length;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form.quantity = null;
      this.form.id = null;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资产型号数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const id = row.id || this.ids;
      // getModels(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改资产型号数据";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      addAssets(this.form).then(response => {
        this.$message({
          message: "新增成功",
          type: "success"
        });
        this.open = false;
        this.getList();
      });
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //       // addAssets(this.form).then(response => {
      //       //    this.$message({
      //       //      message: "修改成功",
      //       //      type: "success"
      //       //    });
      //       //    this.open = false;
      //       //    this.getList();
      //       //  });
      //     } else {
      //
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteAssets({ id: row.id, tag: "model" }).then(res => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // const queryParams = this.queryParams;
      // this.$modal.confirm("是否确认导出所有vadmin/assets/数据项？").then(() => {
      //   this.exportLoading = true;
      //   return exportModels(queryParams);
      // }).then(response => {
      //   this.$download.name(response.msg);
      //   this.exportLoading = false;
      // }).catch(() => {});
    }
  }
};
</script>
<style>
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  margin-top: 15px;
}
</style>
